﻿using System;
using System.Data;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Common;
using Visits.Interface;
using Visits.Model;


namespace Visits.Presenter
{
    public class GraphicsVisitPresenter
    {
        private IGraphicsVisitView _view;
        private GraphicsVisitModel model;
        private BindingSource bindingSource;

        public GraphicsVisitPresenter (IGraphicsVisitView view)
        {

            model = new GraphicsVisitModel();
            bindingSource = new System.Windows.Forms.BindingSource(model.Visit, "GraphicsVisits");
            _view = view;
            _view.SetBindingSource(bindingSource);

            

            view.EditRecord += EditRecord;
            view.DeleteRecord += DeleteRecord;
            view.SaveChanges += SaveChanges;
            Filter[] f = new Filter[]
                             {
                                 new Filter("Всі", "*"), 
                                 new Filter("Понеділок", "Monday"),
                                 new Filter("Вівторок", "Tuesday"), 
                                 new Filter("Середа", "Wednesday"),
                                 new Filter("Четвер", "Thursday"),
                                 new Filter("Пятниця", "Friday"), 
                                 new Filter("Субота","Saturday"), 
                                 new Filter("Неділя","Sunday"), 
                             };
            _view.SetSelectFilter(f);
            _view.SetFilter += SetFilter;

            _view.AddRecord += AddRecord;

        }

        public void AddRecord (object sender, EventArgs e)
        {
            UserControl control = new AddVisitDialog();
            _view.ShowDialog(control);
        }

        public void EditRecord(object sender, EventArgs e)
        {
            //int col = _view.ActiveColumn;
            //if (ts.GridColumnStyles[col] is DataGridCheckBoxColumn)
            //{
            //    string colMap = ts.GridColumnStyles[col].MappingName;
            //    DataRowView drv = (DataRowView)bindingSource.Current;
            //    bool val = !((string) drv[colMap] != "" && (string) drv[colMap] == "True");
            //    drv[colMap] = val;
            //    bindingSource.EndEdit();
            //    _view.IsChange = true;
            //}
        }

        public void SetFilter (object sender, EventArgs e)
        {
            Filter filter = _view.FilterValue;
            if (filter.Value == "*")
            {
                bindingSource.RemoveFilter();
            }
            else
                bindingSource.Filter = filter.Value + " = 1";
        }

        public void DeleteRecord (object sender, EventArgs e)
        {
            bindingSource.RemoveCurrent();
            _view.IsChange = true;
        }

        public void SaveChanges (object sender, EventArgs e)
        {
            bindingSource.EndEdit();
            model.SaveChanges();
        }
    }
}
